import 'package:flutter/material.dart';
import 'package:flutter01/GetX/getController/counterController.dart';
import 'package:flutter01/GetX/middleware/permissionMiddleWare.dart';
import 'package:flutter01/GetX/routes/getcontroller.dart';
import 'package:flutter01/GetX/routes/prod.dart';
import 'package:flutter01/GetX/routes/setting.dart';
import 'package:flutter01/GetX/routes/user.dart';
import 'package:get/get.dart';
import 'package:get/get_rx/get_rx.dart';
import 'package:get/get_rx/src/rx_types/rx_types.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      theme: ThemeData(
        useMaterial3: false,
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: const Text("Flutter APP"),
        ),
        body: const MyHome(),
      ),
      //Get设置默认的路由动画
      defaultTransition: Transition.native,
      //GetX使用getPages来配置路由
      getPages: [
        //可以指定中间件,进行一些权限拦截操作
        GetPage(name: "/user", page: () => const UserPage(),middlewares: [PermissionMiddleWare()]),
        //transition可以指定路由动画
        GetPage(name: "/prod", page: () => const ProdPage(),transition: Transition.leftToRight),
        //路由传递参数也无需其他额外配置
        GetPage(name: "/setting", page: () => const SettingPage()),
        //路由传递参数也无需其他额外配置
        GetPage(name: "/getcontroller", page: () => const GetControllerPage()),
      ],
    );
  }
}


class MyHome extends StatefulWidget {
  const MyHome({super.key});

  @override
  State<MyHome> createState() => _MyHomeState();
}

class _MyHomeState extends State<MyHome> {


  //创建controller并注入Get中
  //可以理解为spring的容器,必须要创建实例对象,添加到容器内,其他地方才能获取到
  CounterController counterController = Get.put(CounterController());

  @override
  Widget build(BuildContext context) {
    print("组件重新build");

    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          ElevatedButton(onPressed: (){
            Get.toNamed("/user");
          }, child: const Text("跳转到User页")),
          ElevatedButton(onPressed: (){
            //GetX 跳转路由
            Get.toNamed("/prod");
          }, child: const Text("跳转到Prod页")),
          ElevatedButton(onPressed: (){
            //GetX 跳转传值
            Get.toNamed("/setting",arguments: {
              "aid":123
            });
          }, child: const Text("跳转到Setting页")),
          ElevatedButton(onPressed: (){
            //GetX 跳转传值
            Get.toNamed("/getcontroller");
          }, child: const Text("跳转到GetController页")),
          ElevatedButton(onPressed: (){
           counterController.inc();
          }, child: const Text("计数+1")),
          //响应式变量需要执行在Obx()方法内
          //rx变量的.value属性才是真正的值对象
          Obx(() => Text("${counterController.counter}",style: const TextStyle(fontSize: 40),))
        ],
      ),
    );
  }
}



